﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using XMK.Tadong.Model;
using XMK.Framework.ElasticSearch;
using XMK.Utils;
using XMK.Framework.Model;

namespace XMK.Tadong.DataSearch.Data
{
    public class XMKFileSearchData
    {
        private ElasticSearchConnection _conn = null;

        public XMKFileSearchData(ElasticSearchConnection connection)
        {
            _conn = connection;
        }

        public List<XMKFileInfo> QueryXmkFileList(IEnumerable<KeyValuePair<string, string>> ps, PageInfo pageInfo)
        {
            List<XMKFileInfo> fileList = new List<XMKFileInfo>();
            string keyw = ps.Where(p => p.Key.ToLower().Equals("keyw")).Select(p => p.Value).FirstOrDefault();
            List<ElasticSearchQuery> queryList = new List<ElasticSearchQuery>();
            if (!string.IsNullOrWhiteSpace(keyw))
                queryList.Add(ElasticSearchQuery.MultiMatch(new string[]{"fILE_NAME", "fILE_CONT"}, keyw, "mmseg"));
            
            string destObj = ps.Where(p => p.Key.ToLower().Equals("dest_obj")).Select(p => p.Value).FirstOrDefault();
            if (!string.IsNullOrWhiteSpace(destObj))
            {
                string[] destArr = destObj.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                //long[] longDestArr = (from dest in destArr select ParseHelper.Parse<long>(dest)).ToArray();
                if (destArr.Length > 0)
                    queryList.Add(ElasticSearchQuery.In("dEST_OBJ", destArr));
            }

            string bizObj = ps.Where(p => p.Key.ToLower().Equals("biz_obj")).Select(p => p.Value).FirstOrDefault();
            if (!string.IsNullOrWhiteSpace(bizObj))
            {
                string[] bizArr = bizObj.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (bizArr.Length > 0)
                    queryList.Add(ElasticSearchQuery.In("bIZ_OBJ", bizArr));
            }

            if (queryList.Count() == 0)
                return null;

            ElasticSearchQuery query = null;
            if (queryList.Count == 1) query = queryList.First();
            else
                query = ElasticSearchQuery.And(queryList.ToArray());

            List<ElasticSearchHighlight> highList = new List<ElasticSearchHighlight>
            {
                new ElasticSearchHighlight
                {
                    FieldName="fILE_NAME",
                    MaxSize = 50,
                    PreTags="<font style='color:red'>",
                    PostTags="</font>"
                },
                new ElasticSearchHighlight
                {
                    FieldName="fILE_CONT",
                    MaxSize = 300,
                    PreTags="<font style='color:red'>",
                    PostTags="</font>"
                }
            };
            

            long recordCount = 0;
            ElasticSearchDataAccess esAccess = new ElasticSearchDataAccess(_conn);
            esAccess.HighLightList = highList;
            var xmkFileInfoList = esAccess.ExecuteQueryResponse<XMKFileInfo>(query, "xmkfileinfo", pageInfo.CurrentPage, pageInfo.PageSize, ref recordCount);
            if (xmkFileInfoList == null)
            {
                pageInfo.Records = 0;
                return null;
            }
            pageInfo.Records = recordCount;

            var resultList = new List<XMKFileInfo>();
            foreach (var item in xmkFileInfoList.Hits)
            {
                XMKFileInfo info = new XMKFileInfo();
                info = item.Source;
                if (item.Highlights.ContainsKey("fILE_NAME"))
                    info.FILE_NAME = item.Highlights["fILE_NAME"].Highlights.FirstOrDefault();
                if (item.Highlights.ContainsKey("fILE_CONT"))
                    info.FILE_CONT = item.Highlights["fILE_CONT"].Highlights.FirstOrDefault();
                else
                    info.FILE_CONT = string.IsNullOrWhiteSpace(info.FILE_CONT) ? "该文档没有找到内容！": string.Concat(info.FILE_CONT.Substring(0, 100), "...");
                resultList.Add(info);
            }

            return resultList;
        }

        public List<XMKFileInfo> SearchXmkFileList(IEnumerable<KeyValuePair<string, string>> ps, PageInfo pageInfo)
        {
            List<XMKFileInfo> fileList = new List<XMKFileInfo>();
            string keyw = ps.Where(p => p.Key.ToLower().Equals("keyw")).Select(p => p.Value).FirstOrDefault();
            List<ElasticSearchQuery> queryList = new List<ElasticSearchQuery>();
            if (!string.IsNullOrWhiteSpace(keyw))
                queryList.Add(ElasticSearchQuery.MultiMatch(new string[] { "fILE_NAME", "fILE_CONT" }, keyw, "mmseg"));

            string destObj = ps.Where(p => p.Key.ToLower().Equals("dest_obj")).Select(p => p.Value).FirstOrDefault();
            if (!string.IsNullOrWhiteSpace(destObj))
            {
                string[] destArr = destObj.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                //long[] longDestArr = (from dest in destArr select ParseHelper.Parse<long>(dest)).ToArray();
                if (destArr.Length > 0)
                    queryList.Add(ElasticSearchQuery.In("dEST_OBJ", destArr));
            }

            string bizObj = ps.Where(p => p.Key.ToLower().Equals("biz_obj")).Select(p => p.Value).FirstOrDefault();
            if (!string.IsNullOrWhiteSpace(bizObj))
            {
                string[] bizArr = bizObj.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (bizArr.Length > 0)
                    queryList.Add(ElasticSearchQuery.In("bIZ_OBJ", bizArr));
            }

            if (queryList.Count() == 0)
                return null;

            ElasticSearchQuery query = null;
            if (queryList.Count == 1) query = queryList.First();
            else
                query = ElasticSearchQuery.And(queryList.ToArray());

            List<ElasticSearchHighlight> highList = new List<ElasticSearchHighlight>
            {
                new ElasticSearchHighlight
                {
                    FieldName="fILE_NAME",
                    MaxSize = 50,
                    PreTags="<font style='color:red'>",
                    PostTags="</font>"
                },
                new ElasticSearchHighlight
                {
                    FieldName="fILE_CONT",
                    MaxSize = 300,
                    PreTags="<font style='color:red'>",
                    PostTags="</font>"
                }
            };


            long recordCount = 0;
            ElasticSearchDataAccess esAccess = new ElasticSearchDataAccess(_conn);
            esAccess.HighLightList = highList;
            var xmkFileInfoList = esAccess.ExecuteQueryResponse<XMKFileInfo>(query, "xmkfileinfo", pageInfo.CurrentPage, pageInfo.PageSize, ref recordCount);
            if (xmkFileInfoList == null)
            {
                pageInfo.Records = 0;
                return null;
            }
            pageInfo.Records = recordCount;

            var resultList = new List<XMKFileInfo>();
            foreach (var item in xmkFileInfoList.Hits)
            {
                XMKFileInfo info = new XMKFileInfo();
                info = item.Source;
                if (item.Highlights.ContainsKey("fILE_NAME"))
                    info.FILE_NAME = item.Highlights["fILE_NAME"].Highlights.FirstOrDefault();
                if (item.Highlights.ContainsKey("fILE_CONT"))
                    info.FILE_CONT = item.Highlights["fILE_CONT"].Highlights.FirstOrDefault();
                else
                    info.FILE_CONT = string.IsNullOrWhiteSpace(info.FILE_CONT) ? "no Content！" : string.Concat(info.FILE_CONT.Substring(0, 100), "...");
                resultList.Add(info);
            }

            return resultList;
        }
    }
}
